* joint_coef_bs_tests.do
* Test if formal effects = informal effects, makes table.

*************************** Change log ********************************** 

/*
	
*/	

clear 
version 13.1

* Project info
cap project, doinfo
if _rc==198 {
	if c(os)=="Unix" loc master "/home/jperez/AA_Minimum_Wage" 
 else if c(username)=="J16339" loc master "B:\Col_Minimum_Wage"
 else if c(username)=="A15858" loc master "B:\Col_Minimum_Wage"
	else loc master "C:/Users/jorpp/Dropbox (Brown)/Col_Minimum_Wage"
 loc pr = 0
}
else {
	local master "`r(pdir)'"
	local doname "`r(dofile)'"  
 loc pr = 1
	* Project calls
	* Problem with linking, need to fix later
	/*
	foreach x in 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 {
		project, uses("`master'\Ster\rif4_new_indcc\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.ster")
		project, uses("`master'\Ster\rif10_new_indcc\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.ster")
		project, uses("`master'\Ster\rif10_new_indcc\jointbs\dta\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.dta")
		
		project, uses("`master'\Ster\rif4_new_ebite_indcc\reg_emp_bp_tq(1999q1)_ebite_salario_mensual_real_50_edom_indcc_p`x'.ster")
		project, uses("`master'\Ster\rif10_new_ebite_indcc\reg_emp_bp_tq(1999q1)_ebite_salario_mensual_real_50_edom_indcc_p`x'.ster")
		project, uses("`master'\Ster\rif4_new_ebite_indcc\jointbs\dta\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.dta")
	}
	*/
}

* get the main coefs

glo qtiles "5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90"
* glo qtiles "5 10"

foreach x in $qtiles {
	qui {
		clear
		est use "`master'\Ster\rif4_new_indcc\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.ster"
		glo formal`x' = _b[treat]
		est use "`master'\Ster\rif10_new_indcc\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.ster"
		glo informal`x' = _b[treat]

		glo diff = $formal`x' - $informal`x'

		* Now look at where this falls in dist

		use "`master'\Ster\rif10_new_indcc\jointbs\dta\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.dta"
		ren * informal_*
		ren informal_initialsort initialsort
		merge 1:1 initialsort using  "`master'\Ster\rif4_new_indcc\jointbs\dta\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.dta"
		drop in 1
		
		/*
		gen diffbs = coef - informal_coef

		sort diffbs
		gen obs=_n
		summ obs if abs(diffbs - $diff) < .001
		local myp = r(mean) / (_N)
		global pval = 2 * min(`myp',(1-`myp'))
		*/
	}
	* di "Percentile = `x' - Pvalue = $pval"
	
	ren coef formal`x'
	ren informal_coef informal`x'
	drop _merge
	keep formal`x' informal`x'
	tempfile f`x'
	save `f`x''
}

clear
use `f5'
foreach x in 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 {
	merge 1:1 _n using `f`x'', nogen
}

glo vars ""
glo stats ""
foreach x in $qtiles {
	glo vars "${vars} formal`x' informal`x'"
	glo stats "${stats} ${formal`x'} ${informal`x'}"
}
	

bstat ${vars}, stat(${stats})

* Global test
test formal5 = informal5
foreach x in 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 {
	test formal`x' = informal`x', accum
}
glo pval : di %9.2f r(p)

texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", replace force
	tex \begin{tabular}{cc}
	tex \toprule
	tex \multicolumn{2}{c}{Fraction Affected}\\
	tex \midrule
	tex Test & P-value \\
	tex \midrule
	tex All Formal = All Informal & ${pval} \\
texdoc close

* Test for selected percentiles

* Formals

test formal5 = informal5
foreach x in 10 15 20 25 {
	test formal`x' = informal`x', accum
}
glo pval : di %9.2f r(p)
texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex Formal p5-p25 = Informal p5-p25 & ${pval} \\
texdoc close
* Informals

test formal40 = informal40
foreach x in 45 50 55 60 65 70 {
	test formal`x' = informal`x', accum
}
glo pval : di %9.2f r(p)
texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex Formal p40-p70 = Informal p40-p70 & ${pval} \\
texdoc close

* Similar around

test formal5 = informal45
test formal10 = informal50, accum
test formal15 = informal55, accum
test formal20 = informal60, accum
test formal25 = informal65, accum
glo pval : di %9.2f r(p)
texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex Formal p5-p25 = Informal p45-p65 & ${pval} \\
texdoc close


foreach x in $qtiles {
	qui {
		clear
		est use "`master'\Ster\rif4_new_ebite_indcc\reg_emp_bp_tq(1999q1)_ebite_salario_mensual_real_50_edom_indcc_p`x'.ster"
		glo formal`x' = _b[treat]
		est use "`master'\Ster\rif10_new_ebite_indcc\reg_emp_bp_tq(1999q1)_ebite_salario_mensual_real_50_edom_indcc_p`x'.ster"
		glo informal`x' = _b[treat]

		glo diff = $formal`x' - $informal`x'

		* Now look at where this falls in dist

		use "`master'\Ster\rif10_new_ebite_indcc\jointbs\dta\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.dta"
		ren * informal_*
		ren informal_initialsort initialsort
		merge 1:1 initialsort using  "`master'\Ster\rif4_new_ebite_indcc\jointbs\dta\reg_emp_bp_tq(1999q1)_bite_salario_mensual_real_50_edom_indcc_p`x'.dta"
		drop in 1
		
		/*
		gen diffbs = coef - informal_coef

		sort diffbs
		gen obs=_n
		summ obs if abs(diffbs - $diff) < .001
		local myp = r(mean) / (_N)
		global pval = 2 * min(`myp',(1-`myp'))
		*/
	}
	* di "Percentile = `x' - Pvalue = $pval"
	
	ren coef formal`x'
	ren informal_coef informal`x'
	drop _merge
	keep formal`x' informal`x'
	tempfile f`x'
	save `f`x''
}

clear
use `f5'
foreach x in 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 {
	merge 1:1 _n using `f`x'', nogen
}

bstat ${vars}, stat(${stats})

* Global test
test formal5 = informal5
foreach x in 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 {
	test formal`x' = informal`x', accum
}
glo pval : di %9.2f r(p)

texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex \midrule
	tex \multicolumn{2}{c}{Fraction At}\\
	tex \midrule
	tex Test & P-value \\
	tex \midrule
	tex All Formal = All Informal & ${pval} \\
texdoc close

* Test for selected percentiles

* Formals

test formal5 = informal5
foreach x in 10 15 20 25 {
	test formal`x' = informal`x', accum
}
glo pval : di %9.2f r(p)
texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex Formal p5-p25 = Informal p5-p25 & ${pval} \\
texdoc close
* Informals

test formal40 = informal40
foreach x in 45 50 55 60 65 70 {
	test formal`x' = informal`x', accum
}
glo pval : di %9.2f r(p)
texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex Formal p40-p70 = Informal p40-p70 & ${pval} \\
texdoc close

* Similar around

test formal5 = informal45
test formal10 = informal50, accum
test formal15 = informal55, accum
test formal20 = informal60, accum
test formal25 = informal65, accum
glo pval : di %9.2f r(p)
texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
	tex Formal p5-p25 = Informal p45-p65 & ${pval} \\
texdoc close


texdoc init "`master'\Doc\tosubmit2\tab\jointbs.tex", append force
tex \bottomrule
tex \end{tabular}
texdoc close

if `pr' project, creates("`master'\Doc\tosubmit2\tab\jointbs.tex")

*cd "`master'\Doc\tosubmit2\tab"
*maketex using jointbs.tex, pack(booktabs)
*! pdflatex "`master'\Doc\tosubmit2\tab\jointbs.tex"



